package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 根据id查询品牌
     * @param id   品牌id
     * @return      品牌信息
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id) {
        //查询
        Brand brand = brandService.getById(id);
        //转换返回
        return ResponseEntity.ok(new BrandDTO(brand));
    }

    /**
     * 根据品牌id集合查询品牌集合
     * @param ids       品牌id集合
     * @return          品牌集合
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids")List<Long> ids) {
        //查询
        List<Brand> brandList = brandService.listByIds(ids);
        //转换
        List<BrandDTO> brandDTOList = brandList.stream().map(BrandDTO::new).collect(Collectors.toList());
        //返回
        return ResponseEntity.ok(brandDTOList);
    }

    /**
     * 分页查询品牌
     * @param page      当前页码
     * @param rows      每页几行
     * @param key       搜索条件
     * @return          品牌信息的分页对象
     */
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "key",required = false)String key) {
        //查询
        PageDTO<BrandDTO> pageDTO = brandService.queryBrandPage(page, rows, key);
        //返回
        return ResponseEntity.ok(pageDTO);
    }

    /**
     * 根据分类id查询品牌
     * @param id        商品分类的id  category_id
     * @return          返回品牌信息
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id")Long id) {
        //查询
        List<BrandDTO> brandDTOList = brandService.queryBrandByCategoryId(id);
        //返回
        return ResponseEntity.ok(brandDTOList);
    }

    /**
     * 新增品牌
     * @param brandDTO      参数为录入的品牌对象
     * @return              新增品牌,无返回值
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新品牌
     * @param brandDTO      点击更新时回显的品牌信息
     * @return              更新品牌,无返回值
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id")Long id) {
        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
